//7. 显示控制模块 - 修复：正确显示时间和数字归零
module display_controller(
    input clk,
    input rst_n,
    input [3:0] current_digit,
    input [15:0] total_time_100ms,
    input [15:0] penalty_time_100ms,
    input [1:0] game_state,
    output reg [15:0] display_4digit,    // 给x7seg模块的16位显示数据
    output reg [3:0] penalty_tens,       // 惩罚时间十位（BCD码）
    output reg [3:0] penalty_ones        // 惩罚时间个位（BCD码）
);
    
    parameter IDLE = 2'b00;
    parameter PLAYING = 2'b01;
    parameter GAME_OVER = 2'b10;
    
    reg [15:0] total_display_time;  // 总显示时间 = 游戏时间 + 惩罚时间
    reg [3:0] time_hundreds, time_tens, time_ones;
    reg [15:0] penalty_display_time;
    
    // 计算总显示时间（直接相加，不做单位转换）
    always @(*) 
    begin
        total_display_time = total_time_100ms + penalty_time_100ms;
        penalty_display_time = penalty_time_100ms;
    end
    
    reg [3:0] time_ones, time_tens, time_hundreds;
reg [9:0] temp_time;

always @(*) begin
    // 百位数字提取
    if (total_display_time >= 10'd900) begin
        time_hundreds = 4'd9;
        temp_time = total_display_time - 10'd900;
    end else if (total_display_time >= 10'd800) begin
        time_hundreds = 4'd8;
        temp_time = total_display_time - 10'd800;
    end else if (total_display_time >= 10'd700) begin
        time_hundreds = 4'd7;
        temp_time = total_display_time - 10'd700;
    end else if (total_display_time >= 10'd600) begin
        time_hundreds = 4'd6;
        temp_time = total_display_time - 10'd600;
    end else if (total_display_time >= 10'd500) begin
        time_hundreds = 4'd5;
        temp_time = total_display_time - 10'd500;
    end else if (total_display_time >= 10'd400) begin
        time_hundreds = 4'd4;
        temp_time = total_display_time - 10'd400;
    end else if (total_display_time >= 10'd300) begin
        time_hundreds = 4'd3;
        temp_time = total_display_time - 10'd300;
    end else if (total_display_time >= 10'd200) begin
        time_hundreds = 4'd2;
        temp_time = total_display_time - 10'd200;
    end else if (total_display_time >= 10'd100) begin
        time_hundreds = 4'd1;
        temp_time = total_display_time - 10'd100;
    end else begin
        time_hundreds = 4'd0;
        temp_time = total_display_time;
    end
    
    // 十位和个位数字提取
    if (temp_time >= 10'd90) begin
        time_tens = 4'd9;
        time_ones = temp_time - 10'd90;
    end else if (temp_time >= 10'd80) begin
        time_tens = 4'd8;
        time_ones = temp_time - 10'd80;
    end else if (temp_time >= 10'd70) begin
        time_tens = 4'd7;
        time_ones = temp_time - 10'd70;
    end else if (temp_time >= 10'd60) begin
        time_tens = 4'd6;
        time_ones = temp_time - 10'd60;
    end else if (temp_time >= 10'd50) begin
        time_tens = 4'd5;
        time_ones = temp_time - 10'd50;
    end else if (temp_time >= 10'd40) begin
        time_tens = 4'd4;
        time_ones = temp_time - 10'd40;
    end else if (temp_time >= 10'd30) begin
        time_tens = 4'd3;
        time_ones = temp_time - 10'd30;
    end else if (temp_time >= 10'd20) begin
        time_tens = 4'd2;
        time_ones = temp_time - 10'd20;
    end else if (temp_time >= 10'd10) begin
        time_tens = 4'd1;
        time_ones = temp_time - 10'd10;
    end else begin
        time_tens = 4'd0;
        time_ones = temp_time[3:0];
    end
end

// 惩罚时间数字提取 (支持0-99) - 单独的always块
reg [3:0] penalty_ones, penalty_tens;

always @(*) begin
    // 十位和个位数字提取
    if (penalty_display_time >= 8'd90) begin
        penalty_tens = 4'd9;
        penalty_ones = penalty_display_time - 8'd90;
    end else if (penalty_display_time >= 8'd80) begin
        penalty_tens = 4'd8;
        penalty_ones = penalty_display_time - 8'd80;
    end else if (penalty_display_time >= 8'd70) begin
        penalty_tens = 4'd7;
        penalty_ones = penalty_display_time - 8'd70;
    end else if (penalty_display_time >= 8'd60) begin
        penalty_tens = 4'd6;
        penalty_ones = penalty_display_time - 8'd60;
    end else if (penalty_display_time >= 8'd50) begin
        penalty_tens = 4'd5;
        penalty_ones = penalty_display_time - 8'd50;
    end else if (penalty_display_time >= 8'd40) begin
        penalty_tens = 4'd4;
        penalty_ones = penalty_display_time - 8'd40;
    end else if (penalty_display_time >= 8'd30) begin
        penalty_tens = 4'd3;
        penalty_ones = penalty_display_time - 8'd30;
    end else if (penalty_display_time >= 8'd20) begin
        penalty_tens = 4'd2;
        penalty_ones = penalty_display_time - 8'd20;
    end else if (penalty_display_time >= 8'd10) begin
        penalty_tens = 4'd1;
        penalty_ones = penalty_display_time - 8'd10;
    end else begin
        penalty_tens = 4'd0;
        penalty_ones = penalty_display_time[3:0];
    end
end
    
    
    
    // 显示控制逻辑 - 修复：游戏结束时数字显示0
    always @(*) 
    begin
        case (game_state)
            IDLE: begin
                // 待机状态显示0000
                display_4digit = 16'h0000;
            end
            
            PLAYING: begin
                // 游戏中：第4位显示当前数字，第1-3位显示当前总计数
                display_4digit = {time_hundreds, time_tens, time_ones, current_digit};
            end
            
            GAME_OVER: begin
                // 游戏结束：第4位显示0（数字归零），第1-3位显示最终总计数
                display_4digit = {time_hundreds, time_tens, time_ones, 4'h0};
            end
            
            default: begin
                display_4digit = 16'h0000;
            end
        endcase
    end
endmodule